package org.jeecg.modules.power.group.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.v1.util.Const;
import org.jeecg.modules.power.group.entity.GroupUser;
import org.jeecg.modules.power.group.entity.SysUserExtends;
import org.jeecg.modules.power.group.entity.YlGroup;
import org.jeecg.modules.power.group.entity.YlGroupUser;
import org.jeecg.modules.power.group.mapper.YlGroupMapper;
import org.jeecg.modules.power.group.service.IYlGroupService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 小组表
 * @Author: jeecg-boot
 * @Date:   2024-07-23
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class YlGroupServiceImpl extends ServiceImpl<YlGroupMapper, YlGroup> implements IYlGroupService {

    /**
     * 根据小组id 获取成员id
     * @param groupId
     * @return
     */
    @Override
    public List<String> getGroupUserById(String groupId) {
        return baseMapper.getGroupUserById(groupId);
    }

    @Override
    public IPage<SysUserExtends > groupUserList(Page<SysUserExtends > page, String username, String groupId) {
        return baseMapper.groupUserList(page, username, groupId);
    }

    @Override
    public YlGroupUser groupUser(String userId, String groupId) {
        return baseMapper.groupUser(userId, groupId);
    }

    /**
     * 根据小组ID查询小组下的BD
     * @param groupId
     * @return
     */
    public List<GroupUser> getGroupUserByGroupId(String groupId){
        return baseMapper.groupUserByGroupId(groupId);
    }

    /**
     * 保存bd小组关系
     * @param id
     * @param userId
     * @param groupId
     */
    @Override
    public void saveGroupUser(String id, String userId, String groupId) {
        baseMapper.saveGroupUser(id, userId, groupId);
    }

    /**
     * 设置组长
     * @param groupUser
     */
    @Transactional
    public void setLeader(GroupUser groupUser){
        baseMapper.cancelLeader(groupUser.getGroupId());
        int res = baseMapper.setLeader(groupUser.getUserId(), groupUser.getGroupId(),Const.leader_1);
        if(res >= 1)this.setLeader(groupUser.getGroupId(),groupUser.getUserId(),groupUser.getUserName());
    }
    /**
     * 取消组长
     * @param groupUser
     */
    @Transactional
    public void cancelLeader(GroupUser groupUser){
        int res = baseMapper.cancelLeader(groupUser.getGroupId());
        if(res >= 1)this.setLeader(groupUser.getGroupId(),"","");
    }
    /**
     * 移出分组
     * @param groupUser
     */
    @Transactional
    public void removeFromGroup(GroupUser groupUser){
        int res = baseMapper.removeFromGroup(groupUser.getUserId(),groupUser.getGroupId());
        if(res >= 1 && Objects.equals(groupUser.getIsLeader(),Const.leader_1))this.setLeader(groupUser.getGroupId(),"","");
    }

    /**
     * 删除小组信息
     * @param userId
     * @return
     */
    public int removeByUserId(String userId){
       return baseMapper.removeByUserId(userId);
    }
    /**
     * 更新小组组长信息
     * @param id
     * @param userId
     */
    public void setLeader(String id,String userId,String username){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("leader_id",userId);
        updateWrapper.set("leader_name",username);
        updateWrapper.eq("id",id);
        this.update(updateWrapper);
    }

    @Override
    public IPage<SysUserExtends> bdUserList(Page<SysUserExtends> page,String username) {
        return baseMapper.bdUserList(page,username);
    }
    /**
     * 获取用户的小组信息
     * @param userId
     */
    public GroupUser getUserGroup(String userId){
        GroupUser groupUser = baseMapper.getGroupUser(userId);
        if( oConvertUtils.isEmpty(groupUser)) return null;
        this.get(groupUser);
        return groupUser;
    }

    public void get(GroupUser groupUser){
        List<JSONObject> list = new ArrayList<>();
        if(Objects.equals(groupUser.getIsLeader(),Const.leader_1)){
            List<GroupUser> groupUserList =  baseMapper.getGroupUsers(groupUser.getGroupId());
            list = groupUserList.stream().map(e->{
                JSONObject object = new JSONObject();
                object.put("userName",e.getUserName());
                object.put("userId",e.getUserId());
                return object;
            }).collect(Collectors.toList());
        }else{
            JSONObject object = new JSONObject();
            object.put("userName",groupUser.getUserName());
            object.put("userId",groupUser.getUserId());
            list.add(object);
        }
        groupUser.setGroupUserList(list);
    }


}
